// pages/partList/partList.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNum: '1',
    canSubmit: true,
    selGoodsList:[],
    partList:{
      chairList:[],
      boxList:[],
      pileAccsessoriesList:[],
      stretcherList:[],
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.getScrollHeight(60)
    })
    if (options.type) {
      this.setData({
        type: options.type,
        selGoodsList: options.partList?JSON.parse(options.partList):[]
      })
    }
    if (options.isSendOut) {
      this.setData({
        isSendOut: true
      })
    }
    if (this.data.type == 'chair') {
      this.setData({
        tabNum: '2'
      })
    }
    if (this.data.type == 'car') {
      this.setData({
        tabNum: '3'
      })
    }
    if (this.data.type == 'box') {
      this.setData({
        tabNum: '4'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    that.toast = that.selectComponent("#toast");
    that.personalModal = that.selectComponent("#personalModal");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPartList()
    this.getStoreList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  stopBubble: function () {
    return
  },
  getPartList: function () {
    let that = this;
    lbbService.getAccessories({}, res => {
      util.print('partList',that.data.partList);
      that.data.partList.chairList = that.selGoodsAddNumInit(res.data.chairList);
      that.data.partList.boxList = that.selGoodsAddNumInit(res.data.boxList);
      that.data.partList.pileAccsessoriesList = that.selGoodsAddNumInit(res.data.pileAccsessoriesList);
      that.data.partList.stretcherList = that.selGoodsAddNumInit(res.data.stretcherList);
      that.setData({
        partList: that.data.partList
      })
    })
  },
  getStoreList: function () {
    let that = this;
    lbbService.getGoods({}, res => {
      let storeList = [];
      Object.keys(res.data).forEach(key => {
        storeList = storeList.concat(res.data[key]);
      })
      that.setData({
        storeList: storeList
      })
    })
  },
  toggleTab: function (e) {
    console.log(e)
    let tabNum = e.currentTarget.dataset.num
    this.setData({
      tabNum
    })
  },
  //增加配件 显示增加配件modal
  showNewPartModal: function () {
    this.setData({
      newPartShow: true
    })
  },
  hideNewPartModal: function () {
    this.setData({
      newPartShow: false
    })
  },
  //提交增加配件表单
  newPart: function (e) {
    console.log("增加表单配件e>>", e)
    let that = this
    if (!that.data.accName) {
      that.toast.showToast('请输入配件名称');
      return
    }
    let accType = that.getType();
    that.hideNewPartModal();
    lbbService.addAccessory({
      accType: accType,
      accName: that.data.accName,
      accUnit: that.data.accUnit || '个'
    }, res => {
      that.getPartList();
    })
  },
  getType: function () {
    let that = this
    if (that.data.tabNum == '1') {
      return '归位桩'
    } else if (that.data.tabNum == '2') {
      return '轮椅'
    } else if (that.data.tabNum == '3') {
      return '担架车'
    } else if (that.data.tabNum == '4') {
      return '控制盒'
    } else {
      return '其他'
    }
  },
  /**
   * 提交选择的零部件并返回上一页面
   */
  formSubmit: function (e) {
    console.log(e)
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 设置初始值
   */
  selGoodsAddNumInit(data){
    let list = this.data.selGoodsList     
    for(var j=0;j<data.length;j++) {
      for (var i = 0; i < list.length; i++) {
         if(data[j].id == list[i].id) {
           data[j].accNum = list[i].accNum;
         }
      }
    }
    return data; 
  },
  accNameInput: function (e) {
    let that = this
    console.log(e)
    that.setData({
      accName: e.detail.value
    })
  },
  accUnitInput: function (e) {
    let that = this
    console.log(e)
    that.setData({
      accUnit: e.detail.value.replace(/[^\u4e00-\u9fa5]+/g,'')
    })
  },
  showModal: function () {
    this.setData({
      isShow: true,
    })
  },
  hideModal: function () {
    this.setData({
      isShow: false,
    })
  },
  checkStore: function (e) {
    console.log(e)
    let that = this
    let accName = e.currentTarget.dataset.accname
    let accId = e.currentTarget.dataset.id
    let accNum = e.detail.value
    if (!accNum) {
      return
    }
    let item = {
      id: accId,
      accNum: accNum,
      accName: accName
    }
    let data = that.checkItemStore(item)
    console.log('data', data)
    if (data.flag == false) {
      that.setData({
        accName: data.accName
      })
      that.showModal()
    }
  },
  checkItemStore: function (item) {
    let that = this
    let storeList = that.data.storeList
    let flag = false
    for (let i = 0; i < storeList.length; i++) {
      console.log('111', item, storeList[i])
      if (item.id == storeList[i].accId) {
        if (parseInt(item.accNum) > parseInt(storeList[i].accNum)) {
          console.log('222')
          that.setData({
            leftNum: storeList[i].accNum
          })
          return {
            flag: false,
            accName: item.accName
          }
        } else {
          console.log('333')
          flag = true
          return {
            flag: true
          }
        }
      }
    }
    console.log('444')
    return {
      flag: false,
      name: item.accName
    }
  },
  selectGoods: function (e) {
    let that = this;
    console.log("选择物品的计数器的数量事件", e);

    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let item = e.currentTarget.dataset.item;
    let selCount = e.detail.value;
    item.goodsNum = selCount;
    let selectGoods = {};
    selectGoods.id = item.id;
    selectGoods.accName = item.accName;
    selectGoods.accUnit = item.accUnit;
    selectGoods.accNum = selCount;
    selectGoods.maximumNum = item.maximumNum;
    //将选择的物品加入购物车      
    let list = this.data.selGoodsList;
    let isExists = false;
    for (var i = 0; i < list.length; i++) {
      let item = list[i];
      if (item.id == selectGoods.id) {
        list[i].accNum = selCount;
        isExists = true;
      }
    }
    if (!isExists) {
      list.push(selectGoods);
    }

    let selList = [];
    for (var i = 0; i < list.length; i++) {
      if (list[i].accNum>0) {
        selList.push(list[i]);
      }
    }

    prevPage.setData({
      selectGoods: selectGoods,
      addGoodsFlag: true,
      selGoodsList:selList
    })

  },
})